package com.hnx.admin.aurora.system.service.impl;

import com.hnx.admin.aurora.annotation.Pager;
import com.hnx.admin.aurora.annotation.RepeatValidator;
import com.hnx.admin.aurora.api.cache.CacheAPI;
import com.hnx.admin.aurora.enums.BusinessType;
import com.hnx.admin.aurora.redis.RedisCache;
import com.hnx.admin.aurora.system.domain.convert.role.RoleConvert;
import com.hnx.admin.aurora.system.domain.dto.role.entity.RoleDTO;
import com.hnx.admin.aurora.system.domain.param.role.form.RoleDelForm;
import com.hnx.admin.aurora.system.domain.param.role.form.RoleForm;
import com.hnx.admin.aurora.system.domain.param.role.query.RoleQuery;
import com.hnx.admin.aurora.system.domain.param.user.form.RoleStateForm;
import com.hnx.admin.aurora.system.domain.param.user.form.UsersRoleForm;
import com.hnx.admin.aurora.system.domain.vo.role.entity.RoleVO;
import com.hnx.admin.aurora.system.mapper.role.RoleMapper;
import com.hnx.admin.aurora.system.mapper.roleDept.RoleDeptMapper;
import com.hnx.admin.aurora.system.mapper.roleMenu.RoleMenuMapper;
import com.hnx.admin.aurora.system.mapper.userRole.UserRoleMapper;
import com.hnx.admin.aurora.system.service.RoleService;
import com.hnx.admin.aurora.web.exception.role.RoleEmptyException;
import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
创建人： 倔强的头发
创建时间：2025/6/17
功能描述:

*/
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService, CacheAPI {
    private final RoleConvert roleConvert;
    private final RoleMapper roleMapper;
    private final RoleMenuMapper roleMenuMapper;
    private final UserRoleMapper userRoleMapper;
    private final RedisCache redisCache;
    private final RoleDeptMapper deptMapper;


    @Override
    public List<RoleVO> getRoleByUserId(String userId) {
        List<RoleDTO> list = roleMapper.getRoleByUserId(userId);
        if (list == null || list.size() == 0) {
            throw new RoleEmptyException();
        }
        return roleConvert.toList(list);
    }

    @Override
    public List<RoleVO> getRoleList() {
        return redisCache.getCacheList(SYS_ROLE_KEY, RoleVO.class).orElseGet(this::getList);
    }

    @Override
    @Pager(orderBy = " role_sort ")
    public ResultJson selectRoleList(RoleQuery role) {
        List<RoleDTO> list = roleMapper.selectRoleList(role);
        List<RoleVO> rlist = roleConvert.toList(list);
        return ResultJson.success().add("rows", rlist);
    }

    @Override
    public ResultJson selectRoleById(String roleId) {
        RoleDTO role = roleMapper.selectRoleById(roleId);
        return ResultJson.success().add("data", role);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @RepeatValidator(type = BusinessType.UPDATE)
    public ResultJson updateRole(RoleForm role) {
        int res = roleMapper.updateRole(role);
        roleMenuMapper.removeRoleMenu(role.getRoleId());
        roleMenuMapper.insertRoleMenu(role.getRoleId(), role.getMenuIds());
        if(res > 0) {
            redisCache.delete(SYS_ROLE_KEY);
        }
        return ResultJson.condition(res);
    }

    @Override
    @RepeatValidator
    @Transactional(rollbackFor = Exception.class)
    public ResultJson insertRole(RoleForm role) {
        int res = roleMapper.insertRole(role);
        roleMenuMapper.insertRoleMenu(role.getRoleId(), role.getMenuIds());
        if(res > 0) {
            redisCache.delete(SYS_ROLE_KEY);
        }
        return ResultJson.condition(res);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultJson deleteRoleByIds(RoleDelForm roleDelForm) {
        int res = roleMapper.deleteRoleByIds(roleDelForm);
        deptMapper.removeRoleDept(roleDelForm.getRoleIds());
        roleMenuMapper.removeRolesMenu(roleDelForm.getRoleIds());
        if(res > 0) {
            redisCache.delete(SYS_ROLE_KEY);
        }
        return ResultJson.condition(res);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultJson authDataScope(RoleForm role) {
        int res = roleMapper.authDataScope(role);
        deptMapper.removeRoleIdDept(role.getRoleId());
        if(role.getDeptIds() != null && role.getDeptIds().length>0) {
            deptMapper.insertRoleDept(role.getRoleId(), role.getDeptIds());
        }
        return ResultJson.condition(res);
    }

    @Override
    public ResultJson insertAuthUsers(UsersRoleForm userRoleForm) {
        int res = userRoleMapper.insertAuthUsers(userRoleForm);
        return ResultJson.condition(res);
    }

    @Override
    public ResultJson deleteAuthUsers(UsersRoleForm usersRoleForm) {
        int res = userRoleMapper.deleteAuthUsers(usersRoleForm);
        return ResultJson.condition(res);
    }

    @Override
    public ResultJson updateRoleStatus(RoleStateForm role) {
        int res = roleMapper.updateRoleStatus(role);
        return ResultJson.condition(res);
    }

    private List<RoleVO> getList() {
        List<RoleDTO> list = roleMapper.selectRoleAll();
        List<RoleVO> roleList = roleConvert.toList(list);
        redisCache.setCacheList(SYS_ROLE_KEY, roleList);
        return roleList;
    }
}
